home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / DEMO_APP / MESSAGE_.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  8.4 KB  |  280 lines

  1. package sub_arctic.demo_apps;
  2.  
  3. import sub_arctic.input.event;
  4. import sub_arctic.input.pick_collector;
  5. import sub_arctic.input.user_info_holder;
  6. import sub_arctic.input.move_draggable;
  7. import sub_arctic.lib.icon;
  8. import sub_arctic.lib.base_interactor;
  9. import sub_arctic.lib.manager;
  10. import sub_arctic.lib.interactor;
  11. import sub_arctic.anim.simple_animatable;
  12. import sub_arctic.anim.simple_transition;
  13. import sub_arctic.anim.line_trajectory;
  14. import sub_arctic.anim.linear_pacer;
  15. import sub_arctic.anim.time_interval;
  16. import sub_arctic.output.loaded_image;
  17.  
  18. import java.awt.Point;
  19. import java.net.MalformedURLException;
  20.  
  21. /**
  22.  * This is the little class that represents the iconic form of a message.
  23.  */
  24.  
  25. public class message_icon extends icon implements move_draggable, simple_animatable {
  26.   /**
  27.    * The message we represent.
  28.    */
  29.   protected message[] _msg;
  30.   /**
  31.    * Our applet 
  32.    */
  33.   mail_test _applet;
  34.   /**
  35.    * The image we are using
  36.    */
  37.   protected loaded_image envelope_image=null;
  38.   /**
  39.    * This is true if we are originating from the headers.
  40.    */
  41.   public boolean _from_headers;
  42.  
  43.   /**
  44.    * Construct an icon.
  45.    */
  46.   public message_icon(message[] m, int global_x, int global_y, 
  47.               base_interactor my_parent,
  48.               mail_test a,
  49.               boolean fh)  {
  50.     super(null);
  51.     _msg=m;
  52.     _applet=a;
  53.     _from_headers=fh;
  54.     if (envelope_image==null) {
  55.       try {
  56.     envelope_image=manager.
  57.       load_doc_image(_applet,"./images/envelope.gif");
  58.       } catch (MalformedURLException e) {
  59.     manager.handle_unexpected_exception(e);
  60.       }
  61.     }
  62.     set_image(envelope_image);
  63.     my_parent.add_child(this);
  64.     set_x(global_x-(envelope_image.width()/2));
  65.     set_y(global_y-(envelope_image.height()/2));
  66.   }
  67.   /**
  68.    * Move ourselves to a new location.
  69.    */
  70.   protected void reposition(int x, int y) {
  71.     set_x(x);
  72.     set_y(y);
  73.   }
  74.   /**
  75.    * Start a drag
  76.    */
  77.   public boolean drag_start(event evt, int x, int y, int grab_x, int grab_y, 
  78.                 Object user_info) {
  79.     reposition(x,y);
  80.     return true;
  81.   }
  82.   /**
  83.    * Continue a drag.
  84.    */
  85.   public boolean drag_feedback(event evt,int x, int y, int start_x, 
  86.                    int start_y, int grab_x, int grab_y, 
  87.                    Object user_info) {
  88.     reposition(x,y);
  89.     return true;
  90.   }
  91.   /**
  92.    * End a drag.
  93.    */
  94.   public boolean drag_end(event evt,int x, int y, int start_x, 
  95.                    int start_y, int grab_x, int grab_y, 
  96.                    Object user_info) {
  97.     pick_collector picker;
  98.     int i;
  99.     user_info_holder uih;
  100.     interactor field, list;
  101.     boolean found_playing_field=false, found_todo_list=false;
  102.  
  103.     /* the object we are going to be looking for is the
  104.      playing field for messages */
  105.     field=_applet.playing_field();
  106.     list=_applet.todo_listbox();
  107.     reposition(x,y);
  108.     /* we might want to pop up a mail message now ... run a pick*/
  109.     picker=new pick_collector();
  110.     /* out parent is the top_level so we pick in that */
  111.     parent().pick(evt.global_x(), evt.global_y(), picker);
  112.     /* see if the object we are looking for is in the list */
  113.     for (i=0; i<picker.num_picks(); ++i) {
  114.       /* get the ith element */
  115.       uih=picker.pick(i);
  116.       /* is this the field?*/
  117.       if (field==uih.obj) {
  118.     found_playing_field=true;
  119.     break;
  120.       }
  121.       /* is this the box */
  122.       if (list==uih.obj) {
  123.     found_todo_list=true;
  124.     break;
  125.       }
  126.     }
  127.     /* ok did we get it ?*/
  128.     if ((found_playing_field==false)  &&
  129.     (found_todo_list==false)) {
  130.       bad_drag(start_x, start_y,x,y);
  131.     } else {
  132.       /* are we coming from the headers? */
  133.       if (_from_headers) {
  134.     /* which did we find? */
  135.     if (found_playing_field) {
  136.       drop_on_playing_field(evt);
  137.     } else {
  138.       drop_on_listbox(evt);
  139.     }
  140.       } else {
  141.     /* we are coming from the list */
  142.       }
  143.     }
  144.     return true;
  145.   }
  146.   /**
  147.    * This function gets called when the transition starts. 
  148.    * @param Object start_obj the start time mapped through the trajectory
  149.    */
  150.   public void start_transition(Object start_obj) {
  151.     /* extract the object */
  152.     Point pt=(Point)start_obj;
  153.     /* reposition this icon */
  154.     reposition(pt.x,pt.y);
  155.   }
  156.   /**
  157.    * This function gets called as the transition progress. You receive
  158.    * a time interval mapped through the pacing and trajectory functions.
  159.    * @param Object start_obj the start time mapped through the trajectory
  160.    * @param Object end_obj the end time mapped through the trajectory
  161.    */
  162.   public void transition_step(Object start_obj, Object end_obj) {
  163.     /* extract the object */
  164.     Point pt=(Point)end_obj;
  165.     /* reposition this icon */
  166.     reposition(pt.x,pt.y);
  167.   }
  168.   /**
  169.    * This function is called when a transition ends.  You receive
  170.    * a time interval mapped through the pacing and trajectory functions.
  171.    * @param Object start_obj the start time mapped through the trajectory
  172.    * @param Object end_obj the end time mapped through the trajectory
  173.    */
  174.   public void end_transition(Object start_obj, Object end_obj) {
  175.     /* extract the object */
  176.     Point pt=(Point)end_obj;
  177.     /* reposition this icon */
  178.     reposition(pt.x,pt.y);
  179.     /* remove us from the tree */
  180.     parent().remove_child(this);
  181.   }
  182.   /**
  183.    * This function gets called when the user drags us somewhere that doesn't
  184.    * make sense. We want to return the origin of the drag.
  185.    */
  186.   public void bad_drag(int origin_x, int origin_y, int current_x, 
  187.                int current_y) {
  188.     long now=time_interval.now(),duration;
  189.     double distance, xdist, ydist;
  190.     
  191.     /* how far is it in each dimension?*/
  192.     xdist=(double)(origin_x-current_x);
  193.     ydist=(double)(origin_y-current_y);
  194.     /* square each dimension */
  195.     xdist=xdist*xdist;
  196.     ydist=ydist*ydist;
  197.     /* compute the square root */
  198.     distance=Math.sqrt(xdist + ydist);
  199.     /* we want 800 pixels to equal 1 second */
  200.     distance/=800.0;
  201.     distance*=1000.0;
  202.     /* round to an int to get the value of the number of ms */
  203.     duration=(long)distance;
  204.  
  205.     /* create pacer ... we want a simple linear mapping */
  206.     linear_pacer pace=new linear_pacer();
  207.     /* move along the line from now to origin */
  208.     line_trajectory traj=new line_trajectory(current_x, current_y,
  209.                          origin_x, origin_y,
  210.                          pace);
  211.     /* the time interval is 100ms + duration whic starts 100ms from now*/
  212.     time_interval interval=new time_interval(now+100,now+200+duration);
  213.     /* tell the animation system we are good to go */
  214.     simple_transition trans=new simple_transition(this,interval,traj);
  215.     manager.animation.schedule_transition(trans);
  216.   }
  217.   /**
  218.    * This method gets called when we get dropped on the playing field.
  219.    */
  220.   public void drop_on_playing_field(event evt) {
  221.     int global_x=evt.global_x(),global_y=evt.global_y();
  222.     Point pt=new Point(global_x,global_y);
  223.     int i;
  224.     message m;
  225.  
  226.     /* do this before we take ourselves out of the hierarchy */
  227.     interactor field=_applet.playing_field();
  228.     /* remove the icon and shove in the display */
  229.     parent().remove_child(this);
  230.     /* set the new coords to be our coords */
  231.     pt=field.global_to_local(pt);
  232.  
  233.     /* loop over all our messages */
  234.     for (i=0; i<_msg.length; ++i) {
  235.       m=_msg[i];
  236.       /* build a message display from this message */
  237.       message_display display=new message_display(m,_applet);
  238.       header_listbox.size_message(display);
  239.       /* this belongs to the playing field */
  240.       field.add_child(display);
  241.       display.set_x(pt.x - (display.w()/2) + (i*5));
  242.       display.set_y(pt.y + (i*5));
  243.     }
  244.   }
  245.   /**
  246.    * This method is called when we have detected a drop on the listbox.
  247.    */
  248.   public void drop_on_listbox(event evt)  {
  249.     event e=new event(evt); // make a copy
  250.     todo_listbox lb=(todo_listbox)_applet.todo_listbox();
  251.     message m;
  252.     int i;
  253.  
  254.     for (i=0; i<_msg.length; ++i) {
  255.       m=_msg[i];
  256.       /* convert to its coordinate system */
  257.       e.global_to_local(lb);
  258.       lb.insert_at_point(m,e.local_y(),-1,-1);
  259.     }
  260.     /* take us off the scrren */
  261.     parent().remove_child(this);
  262.   }
  263. }
  264. /*=========================== COPYRIGHT NOTICE ===========================
  265.  
  266. This file is part of the subArctic user interface toolkit.
  267.  
  268. Copyright (c) 1996 Scott Hudson and Ian Smith
  269. All rights reserved.
  270.  
  271. The subArctic system is freely available for most uses under the terms
  272. and conditions described in 
  273.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  274. and appearing in full in the lib/interactor.java source file.
  275.  
  276. The current release and additional information about this software can be 
  277. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  278.  
  279. ========================================================================*/
  280.